package ex1;


import ex1.CVSection;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import ex1.CurriculumVitae;

public class CVGui extends JFrame implements ActionListener{

	private CurriculumVitae cv;
	
	// Steuerelemente für cvContent
	DefaultTableModel tableModelProperties = new DefaultTableModel();
	
	private JTextField txtSectionName;
	private final ButtonGroup radioButtonGroup = new ButtonGroup();
	private JTextField txtProp1;
	private JTextField txtProp2;
	private JTextField txtProp3;
	private JTextField txtProp4;
	private JTextField txtProp5;
	private JTextField txtProp6;

	private JTable tableProperties = new JTable(tableModelProperties);
	private JRadioButton rdrCVLine = new JRadioButton("CVLine");
	private JRadioButton rdrCVEntry = new JRadioButton("CVEntry");
	private JButton btnAddSection = new JButton("Add section");
	private JButton btnAddProperty = new JButton("Add Property");
	private JComboBox<CVSection> comboType = new JComboBox<CVSection>();
	
	
	public CurriculumVitae getCv() {
		return cv;
	}

	public void setCv(CurriculumVitae cv) {
		this.cv = cv;
	}

	public CVGui() {
		
		// Exercise 1 : GUI-Erweiterung
		
		super("CurriculumVitae");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(640, 480);
		
		// initialize
		this.cv = new CurriculumVitae("filePath");
		
		
		JPanel mainPanel = new JPanel( new BorderLayout());
		this.setContentPane(mainPanel);
		
		JPanel lsc = new JPanel();
		
		JButton load = new JButton("Load CV");
		load.addActionListener(this);
		lsc.add(load);

		JButton save = new JButton("Save CV");
		save.addActionListener(this);
		lsc.add(save);
		
		JButton create = new JButton("Create CV");
		create.addActionListener(this);
		lsc.add(create);

		this.add( lsc, BorderLayout.SOUTH );
		
		JTabbedPane jtp = new JTabbedPane();
		
		this.add(jtp, BorderLayout.CENTER);
		
		// Exercise 2 : Contact Details
		
		JPanel contactDetails = new JPanel();
		//TODO Textfelder für (Name, Adresse, Email, etc.) hinzufügen
		jtp.addTab("Contact Details", contactDetails);
		
		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		c.insets = new Insets(3,3,3,3);
		contactDetails.setLayout(gridbag);
		
		JButton addPic = new JButton("addPic");
		addPic.addActionListener(this);
		
		c.gridx = 0;                        // x-Position
		c.gridy = 5;                        // y-Position
		c.gridwidth = 1;                    // Breite
		c.gridheight = 1;                   // Höhe
		gridbag.setConstraints(addPic, c);
		contactDetails.add(addPic);
		
		JTextField tf_FN = new JTextField("First name");
		
		c.gridx = 0;                        // x-Position
		c.gridy = 0;                        // y-Position
		c.gridwidth = 5;                    // Breite
		c.gridheight = 1;                   // Höhe
		gridbag.setConstraints(tf_FN, c);
		contactDetails.add(tf_FN);
		
		JTextField tf_LN = new JTextField("Last name");
		
		c.gridx = 5;                        // x-Position
		c.gridy = 0;                        // y-Position
		c.gridwidth = 5;                    // Breite
		c.gridheight = 1;                   // Höhe
		gridbag.setConstraints(tf_LN, c);
		contactDetails.add(tf_LN);
		
		JTextField tf_Street = new JTextField("Street");
		
		c.gridx = 0;                        // x-Position
		c.gridy = 1;                        // y-Position
		c.gridwidth = 9;                    // Breite
		c.gridheight = 1;                   // Höhe
		gridbag.setConstraints(tf_Street, c);
		contactDetails.add(tf_Street);
		
		JTextField tf_No = new JTextField("No");
		
		c.gridx = 9;                        // x-Position
		c.gridy = 1;                        // y-Position
		c.gridwidth = 1;                    // Breite
		c.gridheight = 1;                   // Höhe
		gridbag.setConstraints(tf_No, c);
		contactDetails.add(tf_No);
		
		// Exercise 3 : CV Preferences
		
		JPanel cvPreferences = new JPanel();
		//TODO Radiobuttons für CVTheme hinzufügen
		//TODO Labels der Radiobuttonfarben ändern
		JLabel label1 = new JLabel("Modern CV Color: ");
		cvPreferences.add(label1);
		
		JRadioButton rb1 = new JRadioButton( "black" );
		rb1.setForeground(Color.black);
		JRadioButton rb2 = new JRadioButton( "blue" );
		rb2.setForeground(Color.blue);
		JRadioButton rb3 = new JRadioButton( "green" );
		rb3.setForeground(Color.green);
		JRadioButton rb4 = new JRadioButton( "gray" );
		rb4.setForeground(Color.gray);
		JRadioButton rb5 = new JRadioButton( "red" );
		rb5.setForeground(Color.red);
		JRadioButton rb6 = new JRadioButton( "orange" );
		rb6.setForeground(Color.orange);
		JRadioButton rb7 = new JRadioButton( "purple" );
		rb7.setForeground(Color.pink);

		rb1.setSelected( true );
		ButtonGroup buttongrp1 = new ButtonGroup();
		buttongrp1.add( rb1 );
		buttongrp1.add( rb2 );
		buttongrp1.add( rb3 );
		buttongrp1.add( rb4 );
		buttongrp1.add( rb5 );
		buttongrp1.add( rb6 );
		buttongrp1.add( rb7 );
		cvPreferences.add( rb1 );
		cvPreferences.add( rb2 );
		cvPreferences.add( rb3 );
		cvPreferences.add( rb4 );
		cvPreferences.add( rb5 );
		cvPreferences.add( rb6 );
		cvPreferences.add( rb7 );
		
		JLabel label2 = new JLabel("Modern CV Style: ");
		cvPreferences.add(label2);
		JRadioButton rb8 = new JRadioButton( "casual" );
		JRadioButton rb9 = new JRadioButton( "classic" );
		JRadioButton rb10 = new JRadioButton( "oldstyle" );
		JRadioButton rb11 = new JRadioButton( "banking" );
		
		rb1.setSelected( true );
		ButtonGroup buttongrp2 = new ButtonGroup();
		buttongrp2.add( rb8 );
		buttongrp2.add( rb9 );
		buttongrp2.add( rb10 );
		buttongrp2.add( rb11 );
		cvPreferences.add( rb8 );
		cvPreferences.add( rb9 );
		cvPreferences.add( rb10 );
		cvPreferences.add( rb11 );
		
		jtp.addTab("CV Preferences", cvPreferences);
		
		// Exercise 3 : CV Content
		JPanel cvContent = new JPanel();
		jtp.addTab("CV Content", cvContent);
		
		tableProperties.setEnabled(false);
		
		tableModelProperties.addColumn("Entry1");
		tableModelProperties.addColumn("Entry2");
		tableModelProperties.addColumn("Entry3");
		tableModelProperties.addColumn("Entry4");
		tableModelProperties.addColumn("Entry5");
		tableModelProperties.addColumn("Entry6");
		
		txtSectionName = new JTextField();
		txtSectionName.setToolTipText("New section title");
		txtSectionName.setColumns(10);
		
		radioButtonGroup.add(rdrCVLine);
		radioButtonGroup.add(rdrCVEntry);
		
		txtProp1 = new JTextField();
		txtProp1.setColumns(10);
		
		txtProp2 = new JTextField();
		txtProp2.setColumns(10);
		
		txtProp3 = new JTextField();
		txtProp3.setColumns(10);
		
		txtProp4 = new JTextField();
		txtProp4.setColumns(10);
		
		txtProp5 = new JTextField();
		txtProp5.setColumns(10);
		
		txtProp6 = new JTextField();
		txtProp6.setColumns(10);
		
		// Disable textBoxes
		txtProp1.setEnabled(false);
		txtProp2.setEnabled(false);
		txtProp3.setEnabled(false);
		txtProp4.setEnabled(false);
		txtProp5.setEnabled(false);
		txtProp6.setEnabled(false);
		
		// Add events
		this.comboType.addItemListener(new ItemListener() 
		{
			@Override
			public void itemStateChanged(ItemEvent e) 
			{
				// Get current cvSection Object
				CVSection currentSection = (CVSection) e.getItem();
				
				// Clear table
				tableModelProperties.setRowCount(0);
				
				// Add lines and entrys to table
				for (String[] item : currentSection.getCvEntrys()) 
				{
					tableModelProperties.addRow(item);
				}
				
				for (String[] item : currentSection.getCvLines()) 
				{
					tableModelProperties.addRow(item);
				}
			}
		});
		
		this.btnAddSection.addMouseListener(new MouseAdapter() 
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				// Create new section object
				CVSection section = new CVSection();
				section.setName(txtSectionName.getText());
				
				// Clear table
				tableModelProperties.setRowCount(0);
				
				// Add section to Curriculum Vitae
				cv.getCvData().getSections().add(section);
				
				// Add section to comboBox
				comboType.addItem(section);
			}
		});
		
		this.btnAddProperty.addMouseListener(new MouseAdapter()
		{
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				if (comboType.getItemCount() == 0)
					return;
				
				// Get current section from comboBox
				CVSection currentSection = (CVSection) comboType.getSelectedItem();
				
				// Add new line or entry to section
				if (rdrCVEntry.isSelected())
				{
					String[] temp = new String[]  
							{ txtProp1.getText(), txtProp2.getText(), txtProp3.getText()
							, txtProp4.getText(), txtProp5.getText(), txtProp6.getText() 
						};
					
					currentSection.getCvEntrys().add(temp);
					tableModelProperties.addRow(temp);
				}
				else 
				{
					String[] temp = new String[] { txtProp1.getText(), txtProp2.getText() };
					
					currentSection.getCvLines().add(temp);
					tableModelProperties.addRow(temp);
				}
			}
		});
		
		this.rdrCVEntry.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) 
			{
				if (!rdrCVLine.isSelected())
				{
					// Enable all txtBoxes
					txtProp1.setEnabled(true);
					txtProp2.setEnabled(true);
					txtProp3.setEnabled(true);
					txtProp3.setText("");
					txtProp4.setEnabled(true);
					txtProp4.setText("");
					txtProp5.setEnabled(true);
					txtProp5.setText("");
					txtProp6.setEnabled(true);
					txtProp6.setText("");
				}
			}
		});
		
		this.rdrCVLine.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) 
			{
				if (!rdrCVEntry.isSelected())
				{
					// Enable all txtBoxes
					txtProp1.setEnabled(true);
					txtProp2.setEnabled(true);
					txtProp3.setEnabled(false);
					txtProp3.setText("");
					txtProp4.setEnabled(false);
					txtProp4.setText("");
					txtProp5.setEnabled(false);
					txtProp5.setText("");
					txtProp6.setEnabled(false);
					txtProp6.setText("");
				}
				
			}
		});
		
		GroupLayout groupLayout = createCVContentGUI(cvContent);
		
		cvContent.setLayout(groupLayout);
		
		this.setVisible(true);
	}


	private GroupLayout createCVContentGUI(JPanel cvContent) 
	{
		// Create New GroupLayout
		GroupLayout groupLayout = new GroupLayout(cvContent);
		
		// Set horizontal Elements
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(txtSectionName, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnAddSection)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rdrCVLine)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(rdrCVEntry)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnAddProperty))
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(txtProp1, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtProp2, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtProp3, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtProp4, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtProp5, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(txtProp6, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(283)
							.addComponent(comboType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(tableProperties, GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)))
					.addContainerGap())
		);
		// Set vertical Elements
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtSectionName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnAddSection)
						.addComponent(rdrCVLine)
						.addComponent(rdrCVEntry)
						.addComponent(btnAddProperty))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtProp1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtProp2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtProp3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtProp4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtProp5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtProp6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(comboType, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(tableProperties, GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
					.addContainerGap())
		);
		return groupLayout;
	}
	
	public static void main(String args[]) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				CVGui cvGui = new CVGui();
			}
		});
	}
	
	public void actionPerformed(ActionEvent arg0) {
		//TODO Funktionen der verschiedenen Button
		switch ( arg0.getActionCommand() ) {
		case "addPic":
			performAddPicAction();
			
			break;
		case "load":
			System.out.println("load_Button pressed.");
			break;
		case "save":
			System.out.println("save_Button pressed.");
			break;
		case "create":
			System.out.println("create_Button pressed.");
			break;
		default:
			break;
		}
		
		
	}

	private void performAddPicAction() {
		System.out.println("addPic_Button pressed.");
		// JFileChooser-Objekt erstellen
        JFileChooser chooser = new JFileChooser();
        // Dialog zum Oeffnen von Dateien anzeigen
        chooser.showOpenDialog(null);
        
        if(chooser.getSelectedFile() == null)
	    	return;
        System.out.println(chooser.getSelectedFile());
        //TODO vollstandige Pfad im Klassenattribut picture des CurriculumVitae-Objektes speichern
	}

}
